from plone.app.registry.browser import controlpanel
from ownio.url.interfaces import IUrlSettings, _
from z3c.form.browser.text import TextFieldWidget
from z3c.form.browser.textlines import TextLinesFieldWidget
from zope.component import queryUtility

class URLSettingsEditForm(controlpanel.RegistryEditForm):
    """
    This is the form used in the Control Panel configlet for short urls.
    """
    schema = IUrlSettings
    label = _(u"URL settings") 
    description = _(u"Please enter the options specified")
    
    def updateFields(self):
        """
        Update of the fields' widgets.
        """
        super(URLSettingsEditForm, self).updateFields()
        self.fields['domain'].widgetFactory = TextFieldWidget
        self.fields['seed'].widgetFactory = TextFieldWidget
        self.fields['shortUrlContent'].widgetFactory = TextLinesFieldWidget
    
    def updateWidgets(self):
        """
        Update of the widgets' settings.
        """
        super(URLSettingsEditForm, self).updateWidgets()
        self.widgets['shortUrlContent'].rows = 8
        self.widgets['shortUrlContent'].style = u'width: 30%;'
    
class URLSettingsControlPanel(controlpanel.ControlPanelFormWrapper):
    """
    The Control Panel Configlet singleton.
    """
    form = URLSettingsEditForm
        